package org.apache.rocketmq.logging.inner.layouts;

import org.apache.rocketmq.logging.inner.Layout;
import org.apache.rocketmq.logging.inner.LoggingEvent;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * %d{yyy-MM-dd HH:mm:ss,SSS} %p %c{1}%L - %m%n
 */
public class DefaultLayout extends Layout {
    @Override
    public String format(LoggingEvent event) {

        StringBuilder sb = new StringBuilder();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,sss");
        String format = simpleDateFormat.format(new Date(event.timeStamp));
        sb.append(format);
        sb.append(" ");
        sb.append(event.getLevel());
        sb.append(" ");
        sb.append(event.getLoggerName());
        sb.append(" - ");
        sb.append(event.getMessage());
        String[] throwableStr = event.getThrowableStr();
        if (throwableStr != null) {
            sb.append("\r\n");
            for (String s : throwableStr) {
                sb.append(s);
                sb.append("\r\n");
            }
        }
        sb.append("\r\n");
        return sb.toString();
    }

    @Override
    public boolean ignoresThrowable() {
        return false;
    }
}